﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ivony.Html;
using Ivony.Html.Parser;
using JoeBlogs;
using System.IO;

namespace FromWebToWordPress
{
    class Program
    {
        static void Main(string[] args)
        {
            GetContentAndInsertWP();
        }

        /// <summary>
        /// 获取网页数据
        /// </summary>
        private static void GetContentAndInsertWP()
        {
            string url = "http://ie.zafu.edu.cn/?cat=127";
            //新建JumonyParser对象
            var parser = new JumonyParser();
            //加载dom页面
            var document = parser.LoadDocument(url);

            var lis = document.Find("ul[class=news_list]").First().Elements();
            int i = 0;
            foreach (var item in lis)
            {
                i++;
                string title = string.Empty;
                string href = string.Empty;
                string content = string.Empty;
                try
                {
                    var tagA = item.Find("a").First();
                    title = tagA.InnerText();
                    href = tagA.Attribute("href").AttributeValue;
                    var contentDoc = parser.LoadDocument(href);
                    var p = contentDoc.Find("ul[class=n95]");
                    //抓取带html格式的文章内容
                    StringBuilder sb = new StringBuilder();
                    foreach (var eachP in p)
                        sb.Append(eachP.InnerHtml());
                    content = sb.ToString();
                    Console.WriteLine("title:{0}", title);
                    Console.WriteLine("-----------------------------------------------------------------------");
                    Console.WriteLine("content:{0}", content);
                    TestToWP(title, content);
                }
                catch (Exception)
                {
                    //发生异常，写入文件
                    FileStream fs = new FileStream("error.txt", FileMode.Append);
                    StreamWriter sw = new StreamWriter(fs);
                    sw.WriteLine(title);
                    sw.Flush();
                    sw.Close();
                    fs.Close();
                }

            }
        }

        /// <summary>
        /// 插入WordPress
        /// </summary>
        /// <param name="title"></param>
        /// <param name="content"></param>
        private static void TestToWP(string title, string content)
        {
            //http://howtofix.pro/how-to-use-joeblog-wordpress-wrapper-tutorial-with-examples/
            string link = "enter you link";
            string username = "enter you username";
            string password = "enter you password";
            //文章插入的分类
            string[] categories = new string[] { "通知公告" };
            var wp = new WordPressWrapper(link + "/xmlrpc.php", username, password);

            var post = new Post();
            //post.DateCreated = DateTime.Parse(date);
            post.Title = title;
            post.Body = content;
            post.Categories = categories;
            //可以添加其他字段
            //CustomField[] fields = new CustomField[]
            //                       {
            //                           new CustomField{ Key="views", Value=author},
            //                           new CustomField{ Key="source", Value=source}
            //                       };
            wp.NewPost(post, true);
        }
    }
}
